package cc.rengu.igas.bsps.core.realize.impl;

import cc.rengu.igas.bsps.common.constant.BspsServiceNameConstant;
import cc.rengu.igas.bsps.common.constant.BspsTreeNodeConstant;
import cc.rengu.igas.bsps.core.realize.TransSuccessHandleService;
import cc.rengu.jradp.asynnotify.AsynNotify;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * @author: zhangxuran
 * @Date: 2020/5/14 9:59
 * @Description: 交易成功时，后续处理
 */
public class TransSuccessHandleServiceImpl implements TransSuccessHandleService {

    RgLogger rglog = RgLog.getLogger(this.getClass().getName());


    @Override
    public void virtAcctAdjustOrMchntD0SettleHandle(UserOrderInfo userOrderInfo,
                                                    MchntOrderInfo mchntOrderInfo,
                                                    PayOrderInfo payOrderInfo) throws Exception {

        /* 异步进行记账或提现 */
        if (AppParamConstant.YES.equals(mchntOrderInfo.getRealSettleFlag())) {
            /* 订单金额大于最小清算金额-商户D0实时结算 */
            rglog.debug("实时结算开始<{}>", payOrderInfo.getSysSeqNum());
            mchntD0Settle(userOrderInfo, mchntOrderInfo);
        } else if ("0".equals(mchntOrderInfo.getFeeInputMode())) {
            rglog.debug("特殊费率商户<{}>,不进行处理.", mchntOrderInfo.getMchntNo());
        } else {
            /* 非实时清算或订单金额小于最小清算金额-支付成功后异步调用虚拟记账接口 */
            rglog.debug("虚拟记账开始<{}>", payOrderInfo.getSysSeqNum());
            virtAcctMultiMchntAdjust(userOrderInfo, mchntOrderInfo, payOrderInfo);
        }

    }


    /*商户虚拟账户记账*/
    @Override
    public void virtAcctMultiMchntAdjust(UserOrderInfo userOrderInfo,
                                         MchntOrderInfo mchntOrderInfo,
                                         PayOrderInfo payOrderInfo) throws Exception {

        /* 实时入账商户异步d0记账 */
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        xmlTreeUtil.setXmlTreeObjectValue(BspsTreeNodeConstant.MCHNT_ORDER_INFO, mchntOrderInfo);
        xmlTreeUtil.setXmlTreeObjectValue(BspsTreeNodeConstant.PAY_ORDER_INFO, payOrderInfo);
        xmlTreeUtil.setXmlTreeObjectValue(BspsTreeNodeConstant.USER_ORDER_INFO, userOrderInfo);
        AsynNotify asynNotify = new AsynNotify();
        int iReturnCode = asynNotify.transactionNotice(BspsServiceNameConstant.ASYN_MCHNT_VIRT_ACCT_MULTI_SERVICE, 1, 0, 0);
        if (iReturnCode != 0) {
            rglog.error("异步调用AsynMchntVirtAcctMultiAdjustService服务提交失败,iReturnCode:<{}>", iReturnCode);
        }

    }

    @Override
    public void mchntD0Settle(UserOrderInfo userOrderInfo, MchntOrderInfo mchntOrderInfo) throws Exception {
        /* 实时入账商户异步d0实时结算 */
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        xmlTreeUtil.setXmlTreeObjectValue(BspsTreeNodeConstant.MCHNT_ORDER_INFO, mchntOrderInfo);
        xmlTreeUtil.setXmlTreeObjectValue(BspsTreeNodeConstant.USER_ORDER_INFO, userOrderInfo);
        AsynNotify asynNotify = new AsynNotify();
        int iReturnCode = asynNotify.transactionNotice(BspsServiceNameConstant.ASYN_MCHNT_D0_SETTLE_SERVICE, 1, 0, 0);
        if (iReturnCode != 0) {
            rglog.error("异步调用AsynMchntD0SettleService服务提交失败,iReturnCode:<{}>", iReturnCode);
        }

    }


    @Override
    public void virtAcctMchntAujustByPreCompOrPreCancel(UserOrderInfo userOrderInfo, MchntOrderInfo mchntOrderInfo, PayOrderInfo payOrderInfo) throws Exception {
        /* 虚拟账户预授权后续处理 */
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        xmlTreeUtil.setXmlTreeObjectValue(BspsTreeNodeConstant.MCHNT_ORDER_INFO, mchntOrderInfo);
        xmlTreeUtil.setXmlTreeObjectValue(BspsTreeNodeConstant.PAY_ORDER_INFO, payOrderInfo);
        xmlTreeUtil.setXmlTreeObjectValue(BspsTreeNodeConstant.USER_ORDER_INFO, userOrderInfo);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.PAY_TYPE, payOrderInfo.getPayType());
        AsynNotify asynNotify = new AsynNotify();
        int iReturnCode = asynNotify.transactionNotice(BspsServiceNameConstant.ASYN_MCHNT_VIRT_ACCT_PRE_HANDLE_SERVICE, 1, 0, 0);
        if (iReturnCode != 0) {
            rglog.error("异步调用ASYN_MCHNT_VIRT_ACCT_PRE_HANDLE_SERVICE服务提交失败,iReturnCode:<{}>", iReturnCode);
        }

    }
}
